package com.jinboe.order.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.jinboe.comm.dao.impl.BaseDaoImpl;
import com.jinboe.order.dao.OrderDao;
import com.jinboe.order.dto.OrderItemDto;
import com.jinboe.order.dto.OrderMainDto;
import com.jinboe.order.util.OrderConstants;

public class OrderDaoImpl extends BaseDaoImpl implements OrderDao{
	
	
	private String queryOrderItemSql="select orderItem.*,baseInfo.* from YB_ORDERITEM orderItem where 1=1  "+
			"left join YB_BASE_INFO baseInfo on baseInfo.MAININFOID=orderItem.ID " +
			"where 1=1  ";
	
	private String queryOrderMainSql="select orderMain.*,baseInfo.* from YB_ORDERMAIN orderMain " +
			"left join YB_BASE_INFO baseInfo on baseInfo.MAININFOID=orderMain.ID " +
			"where 1=1  ";
	
	private String queryOrderMainCountSql = "select count(orderMain.ID) "+NoteCountName+" from YB_ORDERMAIN orderMain " +
			"left join YB_BASE_INFO baseInfo on baseInfo.MAININFOID=orderMain.ID " +
			"where 1=1  ";
	
	
	@Override
	public String insertOrderMain(OrderMainDto orderMainDto) {
		// TODO Auto-generated method stub
		orderMainDto.setId(super.insertBaseInfo());
		String insertMainSql="Insert into YB_ORDERMAIN (ORDERNAME,PAYCOST,DISCOUNT,STATS,CUSTOMERID,ID) " +
			"values (?,?.?,?,?,?,?,?,?)"; 
		getJdbcTemplate().update(insertMainSql,allMainDtoProperty(orderMainDto));
		return orderMainDto.getId();
	}
	
	@Override
	public String insertOrderItem(OrderItemDto rderItemDto) {
		// TODO Auto-generated method stub
		rderItemDto.setId(super.insertBaseInfo());
		String insertItemSql="Insert into YB_ORDERITEM (MAINID,PRODUCTID,UNITPRICE,DISCOUNT,AMOUNT,REMARK,ID)" +
			" values (?,?,?,?,?,?,?)";
		getJdbcTemplate().update(insertItemSql,allItemDtoProperty(rderItemDto));
		return rderItemDto.getId();
	}
	@Override
	public int updateOrderMain(OrderMainDto orderMainDto) {
		// TODO Auto-generated method stub
		String updateMainSql="update YB_ORDERMAIN set ORDERNAME=?,PAYCOST=?,DISCOUNT=?," ;
		if(orderMainDto.getStats()!=null&&orderMainDto.getStats()==OrderConstants.VALUE_ORDERSTATS_ORDEROVER){
			updateMainSql+="PAYTIME=sysdate";
		}
		updateMainSql+="STATS=?,CUSTOMERID=? where ID=?  ";
		return getJdbcTemplate().update(updateMainSql,allMainDtoProperty(orderMainDto));
	}
	@Override
	public List<OrderItemDto> queryOrderItem(Map conMap) {
		// TODO Auto-generated method stub
		List<OrderItemDto> orderItemList=new ArrayList();
		String sql=createOrderItemWhereConSql(queryOrderItemSql, conMap);
		List queryList = getJdbcTemplate().queryForList(sql);
		Iterator iterator = queryList.iterator();
		while (iterator.hasNext()) {
			Map queryIteratorMap = (Map) iterator.next();
			OrderItemDto orderItemDto=new OrderItemDto();
			createDto(orderItemDto, queryIteratorMap);
			orderItemList.add(orderItemDto);
		}
		return orderItemList;
	}
	
	
	
	@Override
	public List<OrderItemDto> queryOrderItem(String orderMainId) {
		// TODO Auto-generated method stubØ
		List<OrderItemDto> orderItemList=new ArrayList();
		Map conMap=new HashMap();
		conMap.put(OrderConstants.KET_NAME_ORDERITEMID, orderMainId);
		String sql=createOrderItemWhereConSql(queryOrderItemSql, conMap);
		List queryList = getJdbcTemplate().queryForList(sql);
		Iterator iterator = queryList.iterator();
		while (iterator.hasNext()) {
			Map queryIteratorMap = (Map) iterator.next();
			OrderItemDto orderItemDto=new OrderItemDto();
			createDto(orderItemDto, queryIteratorMap);
			orderItemList.add(orderItemDto);
		}
		return orderItemList;
	}
	@Override
	public List<OrderMainDto> queryOrderMain(Map conMap, int startNum, int endNum) {
		// TODO Auto-generated method stub
		List<OrderMainDto> mainInfoList=new ArrayList();
		String sql=createOrderMainWhereConSql(queryOrderMainSql, conMap);
		sql=createPageSql(sql, startNum, endNum);
		List queryList = getJdbcTemplate().queryForList(sql);
		Iterator iterator = queryList.iterator();
		while (iterator.hasNext()) {
			Map queryIteratorMap = (Map) iterator.next();
			OrderMainDto orderMainDto=new OrderMainDto();
			createDto(orderMainDto, queryIteratorMap);
			mainInfoList.add(orderMainDto);
		}
		return mainInfoList;
	}

	@Override
	public int queryOrderMainCount(Map conMap) {
		// TODO Auto-generated method stub
		int noteCount=0;
		String sql=createOrderMainWhereConSql(queryOrderMainCountSql, conMap);
		List queryList = getJdbcTemplate().queryForList(sql);
		Iterator iterator = queryList.iterator();
		while (iterator.hasNext()) {
			Map itMap = (Map) iterator.next();
			noteCount=Integer.parseInt(itMap.get(NoteCountName).toString());
		}
		return noteCount;
	}

	
	public OrderMainDto queryOrderMain(String orderMainId){
		OrderMainDto orderMainDto=new OrderMainDto();
		Map conMap=new HashMap();
		conMap.put(OrderConstants.KET_NAME_ORDERITEMID, orderMainId);
		String sql=createOrderMainWhereConSql(queryOrderMainSql, conMap);
		List queryList = getJdbcTemplate().queryForList(sql);
		Iterator iterator = queryList.iterator();
		while (iterator.hasNext()) {
			Map queryIteratorMap = (Map) iterator.next();
			createDto(orderMainDto, queryIteratorMap);
			
		}
		return orderMainDto;
	}
	//-------------------------private--------------------------------------
	private String createOrderItemWhereConSql(String queryListSql,Map conMap){
		String sql = createBaseWhereConSql(queryListSql, conMap);
		String orderItemId=(String)conMap.get(OrderConstants.KET_NAME_ORDERITEMID);
		String orderMainId=(String)conMap.get(OrderConstants.KET_NAME_ORDERMAINID);
		if(StringUtils.isNotBlank(orderItemId)){
			sql+=" and  orderItem.ID='"+orderItemId.trim()+"' ";
		}
		if(StringUtils.isNotBlank(orderMainId)){
			sql+=" and  orderItem.MAINID='"+orderMainId.trim()+"' ";
		}
		return sql;
	}
	private String createOrderMainWhereConSql(String queryListSql,Map conMap){
		String sql =createBaseWhereConSql(queryListSql, conMap);
		String orderMainId=(String)conMap.get(OrderConstants.KET_NAME_ORDERMAINID);
		if(StringUtils.isNotBlank(orderMainId)){
			sql+=" and  orderMain.ID='"+orderMainId.trim()+"' ";
		}
		return sql;
	}
	private Object [] allMainDtoProperty(OrderMainDto orderMainDto){
		if(orderMainDto==null){
			return null;
		}
		return new Object[] {orderMainDto.getOrderName(),orderMainDto.getPayCost(),
				orderMainDto.getDiscount(),orderMainDto.getStats(),orderMainDto.getCustomerId(),orderMainDto.getId()};
	}
	private Object [] allItemDtoProperty(OrderItemDto rderItemDto){
		if(rderItemDto==null){
			return null;
		}
		
		return new Object[] {rderItemDto.getMainId(),rderItemDto.getProductId(),rderItemDto.getUnitPrice(),
				rderItemDto.getDiscount(),rderItemDto.getAmount(),rderItemDto.getRemark(),rderItemDto.getId()};
	}
}
